﻿using Kimd.Common;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Kimd.Communication
{
    public class CommunicationHelper
    {
        private object _lock = new object();
        private object sendLock = new object();
        private ICommunication _comm;
        private List<SendPackage> _sendPackages;
        public string Description { get; set; }
        public event Action<ReceivePackage> NotifyMessageReceived;
        public CommunicationHelper(string description, ICommunication comm)
        {
            this._comm = comm;
            this._comm.OnMessageReceived = messageReceived;
            this.Description = description;
            this._sendPackages = new List<SendPackage>();
            int hs = this._sendPackages.GetHashCode();
        }
        private void messageReceived(string message)
        {
            try
            {
                if (!message.Contains("CyWarn")&&!message.Contains("RobotCT"))
                {
                    int hs = _sendPackages.GetHashCode();
                    SendPackage sp = _sendPackages.Find(s =>
                    {
                        if (s.PairRule == null)
                            return true;
                        return s.PairRule.Invoke(message);
                    });
                    if (sp != null)
                    {
                        int hh = sp.GetHashCode();
                        sp.RecvStr = message;
                        sp.SendFlag = true;
                        _sendPackages.Remove(sp);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Instance.Log("system", "数据解析异常", MessageLevel.Error, ex);
            }

            NotifyMessageReceived?.Invoke(new ReceivePackage()
            {
                TimeStamp = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
                Message = message,
            });
        }
        public void AddPackage(SendPackage sendPackage)
        {
            _sendPackages.Add(sendPackage);
        }

        public void ClearPackage()
        {
            _sendPackages.Clear();
        }
        public void RemovePackage(SendPackage sendPackage)
        {
            _sendPackages.Remove(sendPackage);
        }
        public void Send(SendPackage sendPackage)
        {
            lock (sendLock)
            {
                sendPackage.SendFlag = false;
                if (sendPackage.SendRule != null)
                    _comm.Send(sendPackage.SendRule(sendPackage.Message));
                else
                    _comm.Send(sendPackage.Message);
                lock (_lock)
                {
                    _sendPackages.Add(sendPackage);
                    int hs = _sendPackages.GetHashCode();
                }
            }
        }
        public bool Connect()
        {
            return _comm.DoConnect();
        }
        public void EmptySendBuffer()
        {
            lock( _lock)
            {
                _sendPackages.Clear();
            }
        }
    }
}
